package mysql

import (
	"manuscript-service/servicerpc/servicerpc"

	"gorm.io/gorm"
)

type Service struct {
	gorm.Model
	Title     string `gorm:"type:varchar(30);not null;comment:标题"`
	Image     string `gorm:"type:varchar(300);not null;comment:图片"`
	LikeCount int    `gorm:"type:int;not null;comment:点赞数"`
	ClassId   int    `gorm:"type:int;not null;comment:类型ID"`
}

// 添加
func (s *Service) ServiceAdd(db *gorm.DB) error {
	return db.Debug().Create(s).Error

}

// 查重
func (s *Service) FindService(db *gorm.DB, title string) error {
	return db.Debug().Where("title=?", title).First(s).Error
}

func ServiceList(db *gorm.DB, list []*servicerpc.ServiceList) []*servicerpc.ServiceList {

	/*
		SELECT
		  services.title,
		  services.image,
		  services.like_count,
		  classes.`name`
		FROM
		  services
		  JOIN classes ON services.class_id = classes.id
	*/
	db.Debug().Model(&Service{}).Select("services.title, services.image,services.like_count, classes.name as class_name").
		Joins("JOIN classes ON services.class_id = classes.id").Find(&list)
	return list
}
